package com.luchao.springbootswager.controller;

import com.luchao.springbootswager.pojo.Data;
import com.luchao.springbootswager.repository.DataRepository;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/data")
@Api("DataController API TEST")
public class DataController {
    @Autowired
    private DataRepository dataRepository;

    @RequestMapping(value="/",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取所有数据", notes = "get请求获取数据")
    public String getall() {
        List list = (List) dataRepository.findAll();
        System.out.println("num:"+list.size());
        return list.toString();
    }
    @RequestMapping(value="/getdatabycidandsid",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "根据cid和sid获取一条数据", notes = "post请求获取一条数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="cid",dataType="int",required=true,value="cid"),
            @ApiImplicitParam(paramType="query",name="sid",dataType="int",required=true,value="sid")
    })
    @ApiResponses({
            @ApiResponse(code=500,message="查询报错！没有数据！")
    })
    public String getdatabycidandsid(Integer cid,Integer sid) {
        Data data =  dataRepository.getDataByCidAndAndSid(cid,sid);
        if(null!=data){
            System.out.println("data:"+data);
            return data.toString();
        }else {
            return "没有数据！";
        }

    }
}
